import matplotlib
matplotlib.use('TkAgg')
import  matplotlib.pyplot as plt
import seaborn as sns
import pandas as pd
import numpy as  np
sns.set_style('darkgrid')

# 中文乱码
# plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['font.sans-serif'] = ['Heiti TC']

df = pd.DataFrame(pd.read_excel('./data/mrtb_data.xlsx'))
plt.rc('font',family='Heiti TC',size=13)
# 按类别分组 , 计算没类实际支付总金额
df1 = df.groupby(['类别'])['买家实际支付金额'].sum()
# 安装 类别 性别 分组,统计每组买家的个数,重置索引
df2 = df.groupby(['类别','性别'])['买家会员名'].count().reset_index()
# 筛选男/女用户数据
man_df = df2[df2['性别'] == '男']
woman_df = df2[df2['性别'] == '女']

# 将数据转换为列表
man_list = list(man_df['买家会员名'])
woman_list = list(woman_df['买家会员名'])
# 将消费金额转换为numpy数组
num = np.array(list(df1))
# 计算男性用户比例
ratio = np.array(man_list) / (np.array(man_list) + np.array(woman_list))
# 精度处理(保留两位小数)
np.set_printoptions(precision=2)
# 就算男性消费金额
man = num * ratio
# 计算女性消费金额
woman = num * (1-ratio)
# 去除重复记录
df3 = df2.drop_duplicates(['类别'])

name = list(df3['类别'])
# ---------- 绘图-----------

x = name # x轴
width = 0.5 # 柱状图宽度
# 标量array[5]   -->  array([1,2,3,4,5])
idx = np.arange(len(x)) #生成x轴位置索引
# 绘制男性用户消费柱状图
plt.bar(idx,man,width,color='slateblue',label='男性用户')
# 绘制女性用户消费柱状图
plt.bar(idx,woman,width,color='orange',label='女性用户')
# 设置坐标轴标签
plt.xlabel('消费类别')
plt.ylabel('男女分布')
# 设置x轴刻度 , 偏移/旋转20  放重叠
plt.xticks(idx + width/2 , x, rotation=20)
# 柱子顶部 添加 数值标签
for a,b in  zip(idx,man):
    plt.text(a,b,'%.0f' % b , ha='center',va='top',fontsize=12)
for a,b,c in  zip(idx,woman,man):
    plt.text(a,b+c+0.5,'%.0f' % b , ha='center',va='bottom',fontsize=12)
# 图例
plt.legend()
plt.show()

